package com.appspot.showup_app.showup;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.ImageButton;

import com.appspot.showup_app.showup.model.ShowUpCustomerMessage;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by eden on 02/12/2014.
 */

public class CustomersActivity extends ActionBarActivity {

    private static final String LOG_TAG = "Showup";
    private static final int EDIT_CUSTOMER_CODE = 0;
    ListView lstCustomers;
    public SQLiteDatabase db;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.customers_agenda);

        lstCustomers = (ListView) findViewById(R.id.lstCustomers);
        DatabaseHelper mDbHelper = new DatabaseHelper(this);
        db = mDbHelper.getWritableDatabase();

        List<ShowUpCustomerMessage> customers = DatabaseHelper.getCustomers(db);
        CustomersAdapter adapter = new CustomersAdapter(this, customers);
        lstCustomers.setAdapter(adapter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_customers, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.menu_action_add) {
            startActivityForResult(new Intent(CustomersActivity.this, EditCustomerActivity.class), EDIT_CUSTOMER_CODE);
        }

        return super.onOptionsItemSelected(item);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // Check which request we're responding to
        if (requestCode == EDIT_CUSTOMER_CODE) {
            // Make sure the request was successful
            if (resultCode == RESULT_OK) {
                List<ShowUpCustomerMessage> customers = DatabaseHelper.getCustomers(db);
                CustomersAdapter adapter = new CustomersAdapter(this, customers);
                lstCustomers.setAdapter(adapter);
            }
        }
    }
}
